package com.aw.guiguzi.adapter;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.aw.guiguzi.R;
import com.aw.guiguzi.bean.AnalysisCombinationItemBean;
import com.aw.guiguzi.util.AppCacheUtil;
import com.aw.guiguzi.util.DataUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class MainCalAnalaysisCombinationAdapter extends BaseQuickAdapter<AnalysisCombinationItemBean, BaseViewHolder> {

    private List<AnalysisCombinationItemBean> mList = new ArrayList<>();

    public MainCalAnalaysisCombinationAdapter() {
        super(R.layout.adapter_main_cal_combination);
        if(mList == null) {
            mList = new ArrayList<>();
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, final AnalysisCombinationItemBean bean) {
        helper.setText(R.id.tv_type, bean.type)
                .setText(R.id.tv_name, bean.name)
                .setText(R.id.tv_countAll, bean.getResultCount(bean.countAll))
                .setText(R.id.tv_pre15, bean.getResultCount(bean.countPre15))
                .setText(R.id.tv_countOutPre, bean.getResultCount(bean.countOutPre15))
                .setText(R.id.tv_limitCount1, bean.getResultCount(bean.countLimit1))
                .setText(R.id.tv_limitCount2, bean.getResultCount(bean.countLimit2))
                .setText(R.id.tv_allRate, bean.getRateAll())
                .setText(R.id.tv_pre15Rate, bean.getPre15Rate())
                .setText(R.id.tv_outPreRate, bean.getRateOutPre())
                .setText(R.id.tv_limit1Rate, bean.getRateLimit1())
                .setText(R.id.tv_limit2Rate, bean.getRateLimit2())
                .setText(R.id.tv_screenOutTitle, "非前" +  AppCacheUtil.getAnalysisNumsScreenOutPreCustom() + "位")
                .setText(R.id.tv_limitAnalysisNumsLengthTitle, "后" +  AppCacheUtil.getAnalysisLengthCustom() + "位")
                .setText(R.id.tv_limitStatisticsCountNumsLengthTitle, "后" +  AppCacheUtil.getLimitStatisticsCountCustom() + "位")
                .setGone(R.id.tv_typeRate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_allRate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_pre15Rate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_outPreRate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_limit1Rate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_limit2Rate, AppCacheUtil.isUseTypePercent());
        if(helper.getLayoutPosition() != 0) {
            if(bean.type.equals(mList.get(helper.getLayoutPosition() - 1).type)) {
                helper.setGone(R.id.tv_type, false)
                        .setGone(R.id.ll_title, false);
            } else {
                helper.setGone(R.id.tv_type, true)
                        .setGone(R.id.ll_title, AppCacheUtil.isStatisticsCount());
            }
        } else {
            helper.setGone(R.id.tv_type, true)
                    .setGone(R.id.ll_title, AppCacheUtil.isStatisticsCount());
        }
        helper.setGone(R.id.ll_content, AppCacheUtil.isStatisticsCount());

        LinearLayout ll_layout = helper.getView(R.id.ll_layout);

        TextView tv_type = helper.getView(R.id.tv_type);
        TextView tv_name = helper.getView(R.id.tv_name);
        tv_type.setText(DataUtil.INSTANCE.matcherSearchText(mContext.getResources().getColor(R.color.black), bean.type, bean.type, true));
        tv_name.setText(DataUtil.INSTANCE.matcherSearchText(mContext.getResources().getColor(com.aw.baselib.R.color.red), bean.name, bean.name, true));
    }

    public void addAllData(List<AnalysisCombinationItemBean> dataList) {
        this.mList = dataList;
        this.setNewData(mList);
        notifyDataSetChanged();
    }

    public void clearData() {
        if(mList != null) {
            int startPosition = 0;//hasHeader is 1
            int preSize = this.mList.size();
            if(preSize > 0) {
                this.mList.clear();
                notifyItemRangeRemoved(startPosition, preSize);
            }
        }
    }
}
